import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    Image,
    TouchableOpacity,
    FlatList,
    SafeAreaView,
    Platform,
    DeviceEventEmitter,
    BackHandler,
    Alert,
} from 'react-native';
import { RRCLoading, RRCToast } from 'react-native-overlayer';
import DeviceInfo from 'react-native-device-info';
import HttpUtil from '../util/HttpUtil';
import DashLine from '../component/DashLine';
import BaseComponent from '../component/BaseComponent';
import RfidModule from '../component/Rfid';
import InfraredModule from '../component/Infrared';
//import KeyEvent from 'react-native-keyevent';
import { NavigationEvents } from 'react-navigation';
import { InventoryTableName, update, queryInventoryDetail, updateInventoryDetailStatus, deleteInventoryMoreOutByInventoryId, insertList, InventoryMoreOutTableName, updateInventoryMoreOut } from '../util/RealmUtil';
import { getPixel, getScreenWidth, getScreenHeight } from '../common/common';
import { v4 as uuidv4 } from 'uuid';
import { bool } from 'prop-types';
let that = {};

export default class InventoryDetail extends BaseComponent {

    constructor(props) {
        super(props);
        this.state = {
            detail: [],
            inventoryed: [],
            surplus: [],
            noInventoryCount: 0,
        }
        that = this;
    }

    componentDidMount() {
        RRCLoading.setLoadingOptions({
            text: '加载中',
            loadingBackgroundColor: 'rgba(0,0,0,0.0)',
            loadingViewStyle: { backgroundColor: 'rgba(0,0,0,0.8)' },
            loadingTextStyle: {}
        })
        RRCLoading.show();
        this.fetchData();
    }

    rfidReceived(codes) {
        // console.log(codes.length);
        // console.log(rfids && rfids.length > 0);
        if (codes != null && codes.length > 0) {
            //console.log("有标签");
            //console.log(codes);
            for (var i = 0; i < codes.length; i++) {
                //console.log("循环" +　codes[i]);
                if (codes[i]) {
                    for (var j = 0; j < that.state.inventoryed.length; j++) {
                        //console.log("已盘循环" + that.state.inventoryed[j].rfidNo);
                        if (that.state.inventoryed[j].rfidNo == codes[i]) {
                            //console.log("已盘过" +　that.state.inventoryed[j].rfidNo + " " + codes[i]);
                            delete codes[i];
                            break;
                        }
                    }
                }
            }
            //console.log("过滤掉已盘");
            for (var i = 0; i < codes.length; i++) {
                if (codes[i]) {
                    for (var j = 0; j < that.state.detail.detailList.length; j++) {
                        if (codes[i] && that.state.detail.detailList[j].rfidNo == codes[i]) {
                            let ele = {
                                assetId: that.state.detail.detailList[j].id_,
                                assetNo: that.state.detail.detailList[j].assetNo,
                                rfidNo: that.state.detail.detailList[j].rfidNo,
                                serialNo: that.state.detail.detailList[j].serialNo,
                            };
                            let inventoryed = Array.from(that.state.inventoryed);
                            inventoryed.push(ele);
                            that.setState({
                                inventoryed: inventoryed,
                            });
                            let noInventoryCount = that.state.noInventoryCount;
                            if (noInventoryCount > 0) {
                                that.setState({ noInventoryCount: noInventoryCount - 1 });
                            }
                            delete codes[i];
                            //console.log(this.state.inventoryed);
                            break;
                        }
                    }
                }
            }

            let surplus = Array.from(that.state.surplus);
            let surplused = false;
            for (var i = 0; i < codes.length; i++) {
                if (codes[i]) {
                    surplused = false;
                    for (var j = 0; j < surplus.length; j++) {
                        if (surplus[j].rfidNo == codes[i]) {
                            surplused = true;
                            break;
                        }
                    }
                    if (!surplused) {
                        let ele = {
                            rfidNo: codes[i],
                        }
                        surplus.push(ele);
                    }
                }
            }
            that.setState({
                surplus: surplus,
            });
        }
    }

    infraredReceived(codes) {
        let isIncludes = false;
        let code = codes[0];
        for (var i = 0; i < that.state.inventoryed.length; i++) {
            if (that.state.inventoryed[i].assetNo == code || that.state.inventoryed[i].rfidNo == code || that.state.inventoryed[i].serialNo == code) {
                isIncludes = true;
                console.log('已经盘点数据' + JSON.stringify(that.state.inventoryed));
                return;
            }
        }

        if (!isIncludes) {
            for (var i = 0; i < that.state.detail.detailList.length; i++) {
                if (that.state.detail.detailList[i].assetNo == code || that.state.detail.detailList[i].rfidNo == code || that.state.detail.detailList[i].serialNo == code) {
                    let ele = {
                        assetId: that.state.detail.detailList[i].id_,
                        assetNo: that.state.detail.detailList[i].assetNo,
                        rfidNo: that.state.detail.detailList[i].rfidNo,
                        serialNo: that.state.detail.detailList[i].serialNo,
                    };
                    let inventoryed = Array.from(that.state.inventoryed);
                    inventoryed.push(ele);
                    that.setState({
                        inventoryed: inventoryed,
                    });
                    let noInventoryCount = that.state.noInventoryCount;
                    if (noInventoryCount > 0) {
                        that.setState({ noInventoryCount: noInventoryCount - 1 });
                    }
                    return;
                }
            }
        }

        let ele = {
            rfidNo: code,
        }

        let surplused = false;
        let surplus = Array.from(that.state.surplus);

        for (var i = 0; i < surplus.length; i++) {
            if (surplus[i].rfidNo == code) {
                surplused = true;
            }
        }
        if (!surplused) {
            surplus.push(ele);
        }
        that.setState({
            surplus: surplus,
        });
    }

    rfidInit() {
        if (DeviceInfo.getDeviceId() === 'rk30sdk') {
            console.log('盘点详情页的红外读头状态:' + InfraredModule.getState());
            InfraredModule.init();
            console.log('盘点详情页红外初始化完成');
            this.infraredListener = DeviceEventEmitter.addListener('onInfraredReveice', this.infraredReceived);
            console.log('盘点详情页始化函数完成')

            if (RfidModule.getState() === 'uninit') {
                RfidModule.init(); // rfid设备初始化
            }
            //console.log('注册按键事件');
            this.keyListener = DeviceEventEmitter.addListener('keydown', function (e) {
                //console.log('keycode: ' + e.which)
                if (e.which === 134) {
                    //console.log('开启扫描111')
                    RfidModule.scan();
                }
                //console.log('keycode: ' + e.which)
            });
            // KeyEvent.onKeyDownListener((keyEvent) => {
            //     //console.log('按键='+keyEvent.keyCode)
            //     if(keyEvent.keyCode === 134){
            //         console.log('开启扫描111')
            //         RfidModule.scan();
            //     }
            // });

            //console.log('注册回调事件');
            this.rfidListener = DeviceEventEmitter.addListener('onRfidReveice', this.rfidReceived);
        }
    }

    componentWillUnmount() {
        if (DeviceInfo.getDeviceId() === 'rk30sdk') {
            //console.log('destory');
            // KeyEvent.removeKeyDownListener();
            if (RfidModule.getState() === 'init') {
                RfidModule.destory();
            }
            if (this.keyListener) {
                this.keyListener.remove();
            }
            if (this.rfidListener) {
                this.rfidListener.remove();
            }
            if (this.infraredListener) {
                this.infraredListener.remove();
                console.log('释放了盘点详情页的红外监听');
            }
        }
        if (Platform.OS === 'android') {
            BackHandler.removeEventListener("hardwareBackPress", this.onBackClicked);
        }
    }

    onBackClicked = () => {
        Alert.alert(
            "提示",
            "确认要退出盘点吗？",
            [
                {
                    text: "取消",
                    onPress: () => console.log("Cancel Pressed"),
                    style: "cancel"
                },
                {
                    text: "确定", onPress: () => {
                        this.props.navigation.navigate('Inventory');
                        this.props.navigation.state.params.callBack();

                    }
                }
            ]
        );
        return true;
    }

    back = () => {
        Alert.alert(
            "提示",
            "确定要退出盘点吗？",
            [
                {
                    text: "取消",
                    onPress: () => console.log("Cancel Pressed"),
                    style: "cancel"
                },
                {
                    text: "确定", onPress: () => {
                        this.props.navigation.navigate('Inventory');
                        this.props.navigation.state.params.callBack();
                    }
                }
            ]
        );
    }
    // 本地模式
    fetchDataFromLocal = () => {
        const { params } = this.props.navigation.state;
        const id = params ? params.id : null;
        queryInventoryDetail(id).then(result => {
            RRCLoading.hide();
            console.log(JSON.stringify(result));
            if (result) {
                let inventoryed = [];
                result.detailList.forEach(value => {
                    if (value.status == 3) {
                        let ele = {
                            assetId: value.id_,
                            assetNo: value.assetNo,
                            rfidNo: value.rfidNo,
                            serialNo: value.serialNo,
                        };
                        inventoryed.push(ele);
                    }
                });
                if (result.moreOutList && result.moreOutList.length > 0) {
                    this.setState({
                        surplus: result.moreOutList
                    })
                }
                this.setState({
                    detail: result,
                    inventoryed: inventoryed,
                    noInventoryCount: result.noInventoryCount,
                });
            } else {
                RRCToast.show('系统异常', 1, 7000);
            }
        }).catch(error => {
            console.log(error);
            RRCLoading.hide();
            RRCToast.show('系统异常', 1, 7000);
        });
    }
    // 连网模式
    fetchDataFromServer = () => {
        const { params } = this.props.navigation.state;
        const id = params ? params.id : null;

        let args = {
            sheetId: id,
        }
        //console.log(id);
        HttpUtil.post('inventoryData', args)
            .then(result => {
                console.log(JSON.stringify(result));
                if (result.httpCode == 200) {
                    console.log('盘点详情页数据' + JSON.stringify(result.data));
                    let inventoryed = [];
                    result.data.detailList.forEach(value => {
                        if (value.status == 3) {
                            let ele = {
                                assetId: value.id_,
                                assetNo: value.assetNo,
                                rfidNo: value.rfidNo,
                                serialNo: value.serialNo,
                            };
                            inventoryed.push(ele);
                        }
                    });
                    if (result.data.moreOutList && result.data.moreOutList.length > 0) {
                        this.setState({
                            surplus: result.data.moreOutList
                        })
                    }
                    this.setState({
                        detail: result.data,
                        inventoryed: inventoryed,
                        noInventoryCount: result.data.noInventoryCount,
                    });
                    //console.log(inventoryed);
                    RRCLoading.hide();
                } else {
                    RRCLoading.hide();
                    RRCToast.show('系统异常', 1, 7000);
                }
            })
            .catch(error => {
                RRCLoading.hide();
                RRCToast.show('系统异常', 1, 7000);
            })
    }
    fetchData = () => {
        if (global.variables.useMode == 1) {
            this.fetchDataFromLocal();
        } else {
            this.fetchDataFromServer();
        }
    }

    renderState(id, state) {
        let isIncludes = false;
        for (var i = 0; i < this.state.inventoryed.length; i++) {
            if (this.state.inventoryed[i].assetId == id) {
                isIncludes = true;
                break;
            }
        }
        if (isIncludes) {
            return <Text style={styles.inventoryed}>已盘</Text>
        } else {
            return <Text style={styles.uninventory}>未盘</Text>
        }

    }

    scan = () => {
        if (DeviceInfo.getDeviceId() === 'rk30sdk') {
            InfraredModule.scan();
        } else {
            DeviceInfo.isCameraPresent()
                .then(isCameraPresent => {
                    if ((isCameraPresent || Platform.OS == 'ios') && DeviceInfo.getDeviceId() !== 'rk30sdk') {
                        this.props.navigation.navigate('QBarCodeScan', {
                            isGoBack: true,
                            isHaveCamera: true,
                            callBack: (code) => {
                                let isIncludes = false;
                                //console.log(code);
                                for (var i = 0; i < this.state.inventoryed.length; i++) {
                                    if (this.state.inventoryed[i].assetNo == code || this.state.inventoryed[i].rfidNo == code || this.state.inventoryed[i].serialNo == code) {
                                        isIncludes = true;
                                        return;
                                    }
                                }

                                //console.log(1);

                                if (!isIncludes) {
                                    //console.log(2);
                                    for (var i = 0; i < this.state.detail.detailList.length; i++) {
                                        if (this.state.detail.detailList[i].assetNo == code || this.state.detail.detailList[i].rfidNo == code || this.state.detail.detailList[i].serialNo == code) {
                                            let ele = {
                                                assetId: this.state.detail.detailList[i].id_,
                                                assetNo: this.state.detail.detailList[i].assetNo,
                                                rfidNo: this.state.detail.detailList[i].rfidNo,
                                                serialNo: this.state.detail.detailList[i].serialNo,
                                            };
                                            let inventoryed = Array.from(this.state.inventoryed);
                                            inventoryed.push(ele);
                                            this.setState({
                                                inventoryed: inventoryed,
                                            });
                                            //console.log(this.state.inventoryed);
                                            return;
                                        }
                                    }
                                }

                                let ele = {
                                    rfidNo: code,
                                }

                                //console.log(3);
                                let surplused = false;
                                let surplus = Array.from(this.state.surplus);

                                for (var i = 0; i < surplus.length; i++) {
                                    if (surplus[i].rfidNo == code) {
                                        surplused = true;
                                    }
                                }
                                if (!surplused) {
                                    surplus.push(ele);
                                }
                                this.setState({
                                    surplus: surplus,
                                });
                                //console.log(surplus);
                            }
                        });
                        // }else if(isCameraPresent && DeviceInfo.getDeviceId() === 'rk30sdk'){
                        //     this.props.navigation.navigate('QBarCodeScan', {
                        //         isGoBack: true,
                        //         isHaveCamera: true,
                        //         callBack: ( code ) => {
                        //             let isIncludes = false;
                        //             //console.log(code);
                        //             for (var i = 0; i < this.state.inventoryed.length; i++) {
                        //                 if(this.state.inventoryed[i].assetNo == code || this.state.inventoryed[i].rfidNo == code || this.state.inventoryed[i].serialNo == code){
                        //                     isIncludes = true;
                        //                     return;            
                        //                 }
                        //             }

                        //             //console.log(1);

                        //             if(!isIncludes){
                        //                 //console.log(2);
                        //                 for (var i = 0; i < this.state.detail.detailList.length; i++) {
                        //                     if(this.state.detail.detailList[i].assetNo == code || this.state.detail.detailList[i].rfidNo == code || this.state.detail.detailList[i].serialNo == code){
                        //                         let ele = {
                        //                             assetId: this.state.detail.detailList[i].id_,
                        //                             assetNo: this.state.detail.detailList[i].assetNo,
                        //                             rfidNo: this.state.detail.detailList[i].rfidNo,
                        //                             serialNo: this.state.detail.detailList[i].serialNo,
                        //                         };
                        //                         let inventoryed = Array.from(this.state.inventoryed);
                        //                         inventoryed.push(ele);
                        //                         this.setState({
                        //                             inventoryed: inventoryed,
                        //                         }); 
                        //                         //console.log(this.state.inventoryed);
                        //                         return; 
                        //                     }
                        //                 }
                        //             }

                        //             let ele = {
                        //                 rfidNo: code,
                        //             }

                        //             //console.log(3);
                        //             let surplused = false;
                        //             let surplus = Array.from(this.state.surplus);

                        //             for (var i = 0; i < surplus.length; i++) {
                        //                 if(surplus[i].rfidNo == code){
                        //                     surplused = true;
                        //                 }
                        //             }
                        //             if(!surplused){
                        //                 surplus.push(ele);
                        //             }
                        //             this.setState({
                        //                 surplus: surplus,
                        //             }); 
                        //             //console.log(surplus);
                        //         }     
                        //     });
                        // }else{
                        //     if(DeviceInfo.getDeviceId() === 'rk30sdk'){
                        //         this.props.navigation.navigate('QBarCodeScan', {
                        //             isGoBack: true,
                        //             isHaveCamera: false,
                        //             isRfid: false, 
                        //             callBack: ( code ) => {
                        //                 let isIncludes = false;
                        //                 //console.log(code);
                        //                 for (var i = 0; i < this.state.inventoryed.length; i++) {
                        //                     if(this.state.inventoryed[i].assetNo == code || this.state.inventoryed[i].rfidNo == code || this.state.inventoryed[i].serialNo == code){
                        //                         isIncludes = true;
                        //                         return;            
                        //                     }
                        //                 }

                        //                 //console.log(1);

                        //                 if(!isIncludes){
                        //                     //console.log(2);
                        //                     for (var i = 0; i < this.state.detail.detailList.length; i++) {
                        //                         if(this.state.detail.detailList[i].assetNo == code || this.state.detail.detailList[i].rfidNo == code || this.state.detail.detailList[i].serialNo == code){
                        //                             let ele = {
                        //                                 assetId: this.state.detail.detailList[i].id_,
                        //                                 assetNo: this.state.detail.detailList[i].assetNo,
                        //                                 rfidNo: this.state.detail.detailList[i].rfidNo,
                        //                                 serialNo: this.state.detail.detailList[i].serialNo,
                        //                             };
                        //                             let inventoryed = Array.from(this.state.inventoryed);
                        //                             inventoryed.push(ele);
                        //                             this.setState({
                        //                                 inventoryed: inventoryed,
                        //                             }); 
                        //                             //console.log(this.state.inventoryed);
                        //                             return; 
                        //                         }
                        //                     }
                        //                 }

                        //                 let ele = {
                        //                     rfidNo: code,
                        //                 }

                        //                 //console.log(3);
                        //                 let surplused = false;
                        //                 let surplus = Array.from(this.state.surplus);

                        //                 for (var i = 0; i < surplus.length; i++) {
                        //                     if(surplus[i].rfidNo == code){
                        //                         surplused = true;
                        //                     }
                        //                 }
                        //                 if(!surplused){
                        //                     surplus.push(ele);
                        //                 }
                        //                 this.setState({
                        //                     surplus: surplus,
                        //                 }); 
                        //                 //console.log(surplus);
                        //             }
                        //         });
                        //     }else{
                        //         RRCToast.show('设备无摄像头或设备异常，无法启动扫描', 2, 3000);
                        //         return;
                        //     }
                    }
                })
                .catch(cameraAccessException => {
                    RRCToast.show('设备无摄像头或设备异常，无法启动扫描', 2, 3000);
                    return;
                });
        }
    }

    submit = () => {

        Alert.alert(
            "提示",
            "确定要保存盘点结果吗？",
            [
                {
                    text: "取消",
                    onPress: () => console.log("Cancel Pressed"),
                    style: "cancel"
                },
                {
                    text: "确定", onPress: () => {
                        const { params } = this.props.navigation.state;
                        const id = params ? params.id : null;
                        let detailList = [];
                        for (var i = 0; i < this.state.inventoryed.length; i++) {
                            detailList.push({ id: this.state.inventoryed[i].assetId, status: 3 });
                        }
                        let args = {
                            id: this.state.detail.id_,
                            inventoryId: id,
                            executeTime: this.state.detail.executeTime,
                            moreOutList: this.state.surplus,
                            detailList: detailList,
                        }
                        RRCLoading.setLoadingOptions({
                            text: '请稍候',
                            loadingBackgroundColor: 'rgba(0,0,0,0.0)',
                            loadingViewStyle: { backgroundColor: 'rgba(0,0,0,0.8)' },
                            loadingTextStyle: {}
                        })
                        RRCLoading.show();
                        if (global.variables.useMode == 1) {
                            this.uploadInventoryToLocal(args);
                        } else {
                            this.uploadInventoryToServer(args);
                        }
                    }
                }
            ]
        );
    }

    uploadInventoryToLocal = async (args) => {
        let isSuccess = true;
        // 盘盈列表
        let moreOutAssetList = [];
        args.moreOutList.forEach((ele) => {
            let obj = {
                id: uuidv4().replace(/-/g, ''),
                inventoryId: args.inventoryId,
                rfidNo: ele.rfidNo,
            }
            moreOutAssetList.push(obj);
        });
        //删除并添加盘盈列表
        await updateInventoryMoreOut(args.inventoryId, moreOutAssetList).then(result => {
            console.log("updateInventoryMoreOut:" + result);
        }).catch(error => {
            console.log(error);
            RRCToast.show('系统异常', 1, 7000);
            isSuccess = false;
        });
        console.log("detailList:" + JSON.stringify(args.detailList));
        // 更新盘点详情状态
        await updateInventoryDetailStatus(args.inventoryId, args.detailList).then(result => {
            console.log("updateInventoryDetailStatus:" + result);
        }).catch(error => {
            console.log(error);
            RRCToast.show('系统异常', 1, 7000);
            isSuccess = false;
        });
        // 更新盘点状态
        let Inventory = {
            id_: args.inventoryId,
            over: args.moreOutList.length,
            normal: args.detailList.length,
            inventoryCount: args.detailList.length,
            noInventoryCount: this.state.noInventoryCount,
            totalCount: (this.state.noInventoryCount + this.state.inventoryed.length + this.state.surplus.length) > 0 ? this.state.noInventoryCount + this.state.inventoryed.length + this.state.surplus.length : 0,
        }
        await update(InventoryTableName, Inventory).then(result => {
            console.log("updateInventoryTable:" + result);
        }).catch(error => {
            console.log(error);
            RRCToast.show('系统异常', 1, 7000);
            isSuccess = false;
        });
        RRCLoading.hide();
        if (isSuccess) {
            RRCToast.show('保存成功', 2, 3000);
            this.props.navigation.navigate('Inventory');
            this.props.navigation.state.params.callBack();
        }
    }


    uploadInventoryToServer = (args) => {
        HttpUtil.post('uploadInventory', args)
            .then(result => {
                if (result.httpCode == 200) {
                    RRCLoading.hide();
                    RRCToast.show('保存成功', 2, 3000);
                    this.props.navigation.navigate('Inventory');
                    this.props.navigation.state.params.callBack();
                } else {
                    RRCLoading.hide();
                    RRCToast.show('系统异常', 1, 7000);
                }
            })
            .catch(error => {
                RRCLoading.hide();
                RRCToast.show('系统异常', 1, 7000);
            })
    }


    renderItem(item) {
        return (
            <View key={item.assetId_} style={{ paddingLeft: getPixel(15), paddingRight: getPixel(15), paddingBottom: getPixel(0), paddingTop: getPixel(10), backgroundColor: '#FFF', marginBottom: getPixel(10) }}>
                <NavigationEvents onDidBlur={() => this.componentWillUnmount()} />
                <View style={styles.cardTitle}>
                    <View style={styles.assetTag}>
                        <Text style={{ fontSize: getPixel(16), color: '#333' }}>
                            {item.assetName}
                        </Text>
                        <Text style={{ fontSize: getPixel(14), color: '#999' }}>
                            {item.rfidNo}
                        </Text>
                    </View>
                    <View style={styles.assetState}>
                        {this.renderState(item.id_, item.status)}
                    </View>
                </View>
                <View style={styles.cardInfo}>
                    <View style={styles.assetInfo}>
                        <View style={styles.assetInfoBasic}>
                            {/* <View style={styles.assetInfoBasicOwner}>
                                    <Text style={{ fontSize: getPixel(16), color:'#333' }}>
                                        {item.manageUserName}
                                    </Text>
                                    <Text style={ styles.assetInfoBasicDept }>
                                        {item.manageBranchName}
                                    </Text>
                                </View> */}
                            <Text style={{ fontSize: getPixel(14), color: '#999' }}>
                                SN: {item.serialNo}
                            </Text>
                        </View>
                        <DashLine backgroundColor='#EEE' len={35} width={240} marginTop={getPixel(10)} marginBottom={getPixel(10)} />
                        <View style={styles.assetInfoOther}>
                            <View style={styles.assetInfoOtherText}>
                                {/* <Text style={{ fontSize: getPixel(14), color:'#999',width: getPixel(135) }}>
                                        位置：{item.roomName}
                                    </Text> */}
                                <Text style={{ fontSize: getPixel(14), color: '#999', }}>
                                    位置：{item.roomName}
                                </Text>
                                {item.startU && item.endU && item.startU != 0 && item.endU != 0 ? <Text style={{ fontSize: getPixel(14), color: '#999' }}>
                                    U位：{item.startU} - {item.endU}
                                </Text> : null}
                            </View>
                            <View style={styles.assetInfoOtherText}>
                                <Text style={{ fontSize: getPixel(14), color: '#999', }}>
                                    详细位置：{item.newRemarks}
                                </Text>
                            </View>
                            <View style={styles.assetInfoOtherText}>
                                {/* <Text style={{ fontSize: getPixel(14), color:'#999',width: getPixel(135) }}>
                                        型号：{item.modelName}
                                    </Text> */}
                                <Text style={{ fontSize: getPixel(14), color: '#999', }}>
                                    型号：{item.modelName}
                                </Text>
                                {item.rackName ? <Text style={{ fontSize: getPixel(14), color: '#999' }}>
                                    机柜：{item.rackName}
                                </Text> : null}
                            </View>
                            <View style={styles.assetInfoOtherText}>
                                <Text style={{ fontSize: getPixel(14), color: '#999' }}>
                                    类型：{item.typeName}
                                </Text>
                            </View>
                            <View style={styles.assetInfoOtherText}>
                                <Text style={{ fontSize: getPixel(14), color: '#999' }}>
                                    货架：{item.location}
                                </Text>
                            </View>
                        </View>
                    </View>
                    <View style={styles.assetImg}>
                        <Image source={item.imgPath ? { uri: item.imgPath } : require('../images/noimg.png')} style={styles.img} />
                    </View>
                </View>
            </View>
        )
    }

    render() {
        return (
            <SafeAreaView style={styles.container}>
                <NavigationEvents onDidFocus={() => this.rfidInit()} />
                <View style={styles.header}>
                    <View style={{ width: getPixel(40), justifyContent: 'center', alignItems: 'center' }}>
                        <TouchableOpacity onPress={() => this.back()}>
                            <Image source={require('../images/back.png')} style={{ resizeMode: 'contain', width: getPixel(25), height: getPixel(25) }} />
                        </TouchableOpacity>
                    </View>

                    <View style={{ width: getScreenWidth() - getPixel(80), justifyContent: 'center', alignItems: 'center' }}>
                        <Text style={{ fontSize: getPixel(17), color: '#333' }}>资产盘点</Text>
                    </View>
                    <View style={{ width: getPixel(40), justifyContent: 'center', alignItems: 'center' }}>
                    </View>
                </View>
                <View style={{ paddingLeft: getPixel(15), paddingRight: getPixel(15), paddingBottom: getPixel(20), paddingTop: getPixel(10), backgroundColor: '#FFF' }}>
                    <View style={styles.title}>
                        <View>
                            <Text style={styles.titleText}>{this.state.detail.inventorySheetName}</Text>
                        </View>
                        <TouchableOpacity onPress={this.submit}>
                            <View style={{ width: getPixel(95), alignItems: 'center' }}>
                                <Text style={styles.stateFinish}>保存结果</Text>
                            </View>
                        </TouchableOpacity>
                    </View>
                </View>
                <View style={styles.info}>
                    <View style={styles.infoText}>
                        <Text style={styles.infoTextTable}>
                            创建人：{this.state.detail.createName}
                        </Text>
                        <Text style={styles.infoTextTable}>
                            盘点总数：{(this.state.noInventoryCount + this.state.inventoryed.length + this.state.surplus.length) > 0 ? this.state.noInventoryCount + this.state.inventoryed.length + this.state.surplus.length : 0}
                        </Text>
                    </View>
                    <View style={styles.infoText}>
                        <Text style={styles.infoTextTable}>
                            创建日期：{this.state.detail.createTimeString}
                        </Text>
                        <Text style={styles.infoTextTable}>
                            完成日期：{this.state.detail.endTimeString}
                        </Text>
                    </View>
                    <View style={styles.detail}>
                        <View style={styles.detailBox}>
                            <Text style={[styles.detailNum, { color: '#4A69DD' }]}>{this.state.noInventoryCount}</Text>
                            <Text style={styles.detailName}>待盘点</Text>
                        </View>
                        <View style={styles.detailBox}>
                            <Text style={styles.detailNum}>{this.state.inventoryed.length}</Text>
                            <Text style={styles.detailName}>已盘点</Text>
                        </View>
                        <View style={styles.detailBox}>
                            <Text style={styles.detailNum}>{this.state.surplus.length}</Text>
                            <Text style={styles.detailName}>盘盈</Text>
                        </View>
                        <View style={[styles.detailBox, { marginRight: getPixel(0) }]}>
                            <Text style={styles.detailNum}>{this.state.detail.loss ? this.state.detail.loss : 0}</Text>
                            <Text style={styles.detailName}>盘亏</Text>
                        </View>
                    </View>
                </View>
                <FlatList
                    style={{ backgroundColor: '#F5F5F5', marginTop: getPixel(10) }}
                    data={this.state.detail.detailList}
                    renderItem={({ item }) => this.renderItem(item)}
                    // onEndReached={this._onEndReached.bind(this)}
                    // onEndReachedThreshold={1}
                    keyExtractor={(item, index) => index.toString()}
                // ListFooterComponent={this._renderFooter.bind(this)}
                >
                </FlatList>

                <View style={styles.buttonGroup}>
                    <TouchableOpacity onPress={this.scan.bind(this)}>
                        <View style={styles.buttonSearch}>
                            <Text style={{ color: '#FFFFFF', textAlign: 'center', fontSize: getPixel(16), lineHeight: getPixel(40) }}>扫一扫</Text>
                        </View>
                    </TouchableOpacity>
                    {/* <TouchableOpacity onPress={this.submit}>
                        <View style={styles.buttonSave}>
                            <Text style={{ color: '#FFFFFF', textAlign: 'center', fontSize: getPixel(16), lineHeight: getPixel(40) }}>新增</Text>
                        </View>
                    </TouchableOpacity>  */}
                </View>
            </SafeAreaView>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5F5F5',
    },
    header: {
        height: getPixel(50),
        elevation: 4,
        shadowOffset: { width: 0, height: 5 },
        shadowOpacity: 0.5,
        shadowRadius: 10,
        shadowColor: '#DDD',
        backgroundColor: '#FFF',
        flexDirection: 'row',
    },
    title: {
        flexDirection: 'row',
        alignItems: 'center',
        borderBottomColor: '#EEE',
        borderBottomWidth: getPixel(1),
        paddingBottom: getPixel(10),
    },
    titleText: {
        fontSize: getPixel(16),
        color: '#333',
        width: getScreenWidth() - getPixel(115),
    },
    stateStart: {
        fontSize: getPixel(14),
        padding: getPixel(4),
        backgroundColor: '#FFAA2E',
        color: '#FFF',
        borderRadius: getPixel(5),
        paddingLeft: getPixel(28),
        paddingRight: getPixel(28),
        width: getPixel(180),
        height: getPixel(30),
    },
    stateFinish: {
        fontSize: getPixel(14),
        padding: getPixel(4),
        backgroundColor: '#4A69DD',
        color: '#FFF',
        borderRadius: getPixel(5),
    },
    inventoryed: {
        fontSize: getPixel(14),
        padding: getPixel(4),
        backgroundColor: '#FFAA2E',
        color: '#FFF',
        borderRadius: getPixel(5),
        paddingLeft: getPixel(8),
        paddingRight: getPixel(8),
    },
    uninventory: {
        fontSize: getPixel(14),
        padding: getPixel(4),
        backgroundColor: '#4A69DD',
        color: '#FFF',
        borderRadius: getPixel(5),
        paddingLeft: getPixel(8),
        paddingRight: getPixel(8),
    },
    info: {
        justifyContent: 'center',
        marginTop: getPixel(10),
        backgroundColor: '#FFF',
        paddingBottom: getPixel(10),
        paddingTop: getPixel(10),
    },
    infoText: {
        flexDirection: 'row',
        alignItems: 'center',
        height: getPixel(30),
        marginLeft: getPixel(15),
    },
    infoTextTable: {
        fontSize: getPixel(14),
        color: '#999',
        width: getScreenWidth() / 2,
    },
    detail: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'center',
    },
    detailBox: {
        width: getScreenWidth() / 4 - getPixel(10),
        height: getScreenWidth() / 4 - getPixel(10),
        backgroundColor: '#FAFAFA',
        marginRight: getPixel(1),
        alignItems: 'center',
        justifyContent: 'center',
        marginRight: getPixel(4),
    },
    detailName: {
        color: '#999',
        paddingTop: getPixel(10),
        fontSize: getPixel(14),
    },
    detailNum: {
        color: '#000',
        fontSize: getPixel(16),
    },
    buttonGroup: {
        flexDirection: 'row',
        paddingLeft: getPixel(15),
        paddingRight: getPixel(15),
        height: getPixel(60),
        backgroundColor: '#FFF',
        width: getScreenWidth(),
        alignItems: 'center',
        elevation: 4,
        shadowOffset: { width: 0, height: 5 },
        shadowOpacity: 0.5,
        shadowRadius: 10,
        shadowColor: '#DDD',
        backgroundColor: '#FFF',
    },
    buttonSearch: {
        backgroundColor: '#6484FA',
        height: getPixel(40),
        borderRadius: getPixel(10),
        width: (getScreenWidth() - 30),
    },
    buttonSave: {
        backgroundColor: '#4a69dd',
        height: getPixel(40),
        borderTopRightRadius: getPixel(10),
        borderBottomRightRadius: getPixel(10),
        width: (getScreenWidth() - 30) / 2,
    },
    cardTitle: {
        flexDirection: 'row',
        borderBottomColor: '#EEEEEE',
        borderBottomWidth: getPixel(1),
    },
    cardInfo: {
        flexDirection: 'row',
        marginTop: getPixel(10),
    },
    assetTag: {
        width: getScreenWidth() - getPixel(110),
        height: getPixel(60),
        justifyContent: 'center',
    },
    assetInfo: {
        width: getScreenWidth() - getPixel(130),
        height: getPixel(130),
        justifyContent: 'center',
    },
    assetImg: {
        width: getPixel(100),
        height: getPixel(130),
        justifyContent: 'center',
        alignItems: 'flex-end',
        marginTop: getPixel(20),
    },
    assetInfoBasic: {
        justifyContent: 'center',
    },
    assetInfoOther: {
        justifyContent: 'center',
    },
    assetInfoOtherText: {
        flexDirection: 'row',
        alignItems: 'center',
    },
    assetInfoBasicOwner: {
        flexDirection: 'row',
        alignItems: 'center',
    },
    assetInfoBasicDept: {
        fontSize: getPixel(12),
        color: '#FFAA2E',
        backgroundColor: '#FFF9EF',
        borderColor: '#FFAA2E',
        borderWidth: 1,
        padding: getPixel(2),
        paddingLeft: getPixel(4),
        marginLeft: getPixel(5),
        height: getPixel(20),
    },
    img: {
        resizeMode: 'contain',
        width: getPixel(100),
        height: getPixel(100),
    },
    assetState: {
        width: getPixel(80),
        height: getPixel(60),
        justifyContent: 'center',
        alignItems: 'flex-end',
    },
    assetStateText: {
        fontSize: getPixel(14),
        width: getPixel(60),
        height: getPixel(30),
        borderColor: '#58CEB6',
        borderWidth: 1,
        textAlign: 'center',
        lineHeight: getPixel(30),
        color: '#58CEB6',
        backgroundColor: '#EEFFFC',
        borderRadius: 10,
    },
});